package com.yilin.tms.order.controller;

import com.yilin.commons.util.DateTimeUtil;
import com.yilin.commons.util.StringUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.order.transit.TransitOrdinaryOrder;
import com.yilin.tms.core.entity.truck.TruckVehicle;
import com.yilin.tms.core.remote.IVehicleRemote;
import com.yilin.tms.order.service.order.impl.TransitOrdinaryService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/order/ordinary/")
public class OrdinaryController extends BaseController {

    @Resource
    private TransitOrdinaryService transitOrdinaryService;
    @Resource
    private IVehicleRemote vehicleRemote;
    /**
     * 保存修改普通运单
     */
    @PostMapping(value = "saveOrdinary")
    public ReturnData<Object> saveOrdinary(TransitOrdinaryOrder transitOrdinaryOrder) {
        ParamUtil.isNull(transitOrdinaryOrder, "订单信息未传入");
        ParamUtil.isNull(transitOrdinaryOrder.getGoodsName(), "不能为空");
        ParamUtil.isNull(transitOrdinaryOrder.getTruckId(), "车辆不能为空");
        ParamUtil.isNull(transitOrdinaryOrder.getLoadingName(), "装货商名称不能为空");
        ParamUtil.isNull(transitOrdinaryOrder.getLoadingAddress(), "装货地址不能为空");
        ParamUtil.isNull(transitOrdinaryOrder.getLoadingImgUrl(), "装货磅单图片不能为空");
        ParamUtil.isNull(transitOrdinaryOrder.getLoadingWeight(), "装货磅单重量不能为空");
        ParamUtil.isNull(transitOrdinaryOrder.getUnloadingName(), "卸货商名称不能为空");
        ParamUtil.isNull(transitOrdinaryOrder.getUnloadingAddress(), "卸货商地址不能为空");
        ParamUtil.isNull(transitOrdinaryOrder.getUnloadingImgUrl(), "卸货磅单图片不能为空");
        ParamUtil.isNull(transitOrdinaryOrder.getUnloadingWeight(), "卸货磅单重量吨不能为空");
        ParamUtil.isNull(transitOrdinaryOrder.getFreightUnitPrice(), "不能为空");
        TruckVehicle truckVehicle=ParamUtil.isSuccess(vehicleRemote.getVehicleById(transitOrdinaryOrder.getTruckId()));
        ParamUtil.isNull(truckVehicle,"获取车辆信息失败");
        transitOrdinaryOrder.setTruckNo(truckVehicle.getTrackNo());
        if(StringUtil.isNotEmpty(truckVehicle.getTractorNo())){
            transitOrdinaryOrder.setTrailerNo(truckVehicle.getTractorNo());
        }
        transitOrdinaryService.saveOrUpdateTransitOrdinary(getLoginUser(), transitOrdinaryOrder);
        return jsonView(SUCCESS_CODE, "更新成功");
    }
    /**
     * 获取运单根据ID
     */
    @PostMapping(value = "getTransitOrdinaryOrderById")
    public ReturnData<TransitOrdinaryOrder> getTransitOrdinaryOrderById(String ordinaryId) {
        ParamUtil.isNull(ordinaryId,"获取订单失败");
        TransitOrdinaryOrder pageData = transitOrdinaryService.getTransitOrdinaryOrderById(ordinaryId);
        return jsonView(SUCCESS_CODE, "获取成功", pageData);
    }

    /**
     * 获取运单分页
     */
    @PostMapping(value = "getTransitOrdinaryPage")
    public ReturnData<PageData<TransitOrdinaryOrder>> getTransitOrdinaryPage(TransitOrdinaryOrder query, Integer page, Integer limit) {
        PageData<TransitOrdinaryOrder> pageData = transitOrdinaryService.getTransitOrdinaryPage(getLoginUser(), query, page, limit);
        return jsonView(SUCCESS_CODE, "获取成功", pageData);
    }

    /**
     * 审核
     */
    @PostMapping(value = "auditingTransitOrdinaryOrder")
    public ReturnData<Object> auditingTransitOrdinaryOrder(String ordinaryId, TransitOrdinaryOrder.TransitStatus transitStatus, String resRemark) {
        ParamUtil.isNull(ordinaryId, "获取订单信息失败");
        ParamUtil.isNull(transitStatus, "获取订单状态失败");
//        if (transitStatus != TransitOrdinaryOrder.TransitStatus.create && transitStatus != TransitOrdinaryOrder.TransitStatus.success) {
//            return jsonView(false, "订单状态错误");
//        }
        if (transitStatus == TransitOrdinaryOrder.TransitStatus.create && StringUtil.isEmpty(resRemark)) {
            return jsonView(false, "请填写失败原因");
        }
        TransitOrdinaryOrder transitOrdinaryOrder = transitOrdinaryService.getTransitOrdinaryOrderById(ordinaryId);
        ParamUtil.isNull(transitOrdinaryOrder, "获取信息订单失败");
        transitOrdinaryOrder.setTransitStatus(transitStatus);
        transitOrdinaryOrder.setReturnRemark(resRemark);
        transitOrdinaryService.auditingTransitOrdinaryOrder(getLoginUser(), transitOrdinaryOrder);
        return jsonView(SUCCESS_CODE, "更新成功");
    }

    /**
     * 付款
     */
    @PostMapping(value = "paymentTransitOrdinaryOrder")
    public ReturnData<Object> paymentTransitOrdinaryOrder(String ordinaryId, String date) {
        ParamUtil.isNull(ordinaryId, "获取订单信息失败");
        TransitOrdinaryOrder transitOrdinaryOrder = transitOrdinaryService.getTransitOrdinaryOrderById(ordinaryId);
        ParamUtil.isNull(transitOrdinaryOrder, "获取信息订单失败");
        if (StringUtil.isNotEmpty(date)) transitOrdinaryOrder.setPaymentDate(DateTimeUtil.stringToDate(date, null));
        transitOrdinaryService.paymentTransitOrdinaryOrder(getLoginUser(), transitOrdinaryOrder);
        return jsonView(SUCCESS_CODE, "更新成功");
    }
    /**
     * 删除
     */
    @PostMapping(value = "deleteTransitOrdinaryOrder")
    public ReturnData<Object> deleteTransitOrdinaryOrder(String ordinaryId) {
        ParamUtil.isNull(ordinaryId, "获取订单信息失败");
        transitOrdinaryService.deleteTransitOrdinaryOrder(ordinaryId);
        return jsonView(SUCCESS_CODE, "更新成功");
    }

}
